import { injectable } from 'tsyringe';
import { IUserService } from '../../domain/interfaces/IUserService';
import type { PublicUser } from '../../domain/types/userTypes';
import { User } from '../../domain/entities/User';

@injectable()
export class UserRepository {
  private users: Map<string, User> = new Map();

  async create(user: User): Promise<void> {
    this.users.set(user.id, user);
  }

  async findById(id: string): Promise<User | null> {
    return this.users.get(id) || null;
  }

  async findByUsername(username: string): Promise<User | null> {
    for (const u of this.users.values()) {
      if (u.username === username) return u;
    }
    return null;
  }

  async listOnline(): Promise<PublicUser[]> {
    const res: PublicUser[] = [];
    for (const u of this.users.values()) {
      if (u.status === (u as any).status) {
        res.push({ id: u.id, username: u.username, status: u.status, lastSeen: u.lastSeen });
      }
    }
    return res;
  }

  async updateStatus(id: string, status: string): Promise<void> {
    const u = this.users.get(id);
    if (!u) return;
    u.status = status as any;
    u.lastSeen = Date.now();
    this.users.set(id, u);
  }
}
